package org.codesketch.flamingo

import java.util.Date;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codesketch.flamingo.ssh.SSHClient;

class Node {

    String name;
	String description;
	String hostname;
	@JsonIgnore
	Integer port=22;
	@JsonIgnore
	Integer adminPort = 6082
	@JsonIgnore
	String username;
	@JsonIgnore
	String password;
	Boolean active;
	
	/*
	 * automatically filled by grails on creation can be disabled adding the static 
	 * constraints autoTimestamp = false
	 */
//	Date dateCreated;
	/*
	* automatically filled by grails on update can be disabled adding the static
	* constraints autoTimestamp = false
	*/
//	Date lastUpdate;
	
	SSHClient client;
	
	/*
	 * One node is linked to a many snapshot.
	 */
//	static hasMany = [snapshot:Snapshot]
	/*
	* Avoid to add the admin field to the database that is needed for the isAdmin method,
	* because admin is not a valid field, that get GORM look into the DB for that field.
	*/
	static transients = ['client', 'connected']
	
    static constraints = {
		name(blank:false, nullable:false, maxSize:20)
		description(maxSize:255)
		hostname(blank:false, nullable:false,  maxSize:255)
		port(min:0, max:99999)
		username(blank:false, nullable:false)
		password(password:true)
    }
	
	def connect() {
		client = new SSHClient()
		client.connect(this)
	}
	/**
	 * Execute the given command on the remote server.
	 * @param command the command to execute.
	 * @return the command execution response as a {@link Response} object.
	 */
	def executeSSHCommand(String command) {
		if(null==client || !client.isConnected()) {
			connect();
		}
		return client.execute(command)
	}
	
	@Override
	public String toString() {
		return "${name}, ${hostname}";
	}
}
